package com.talos.ums.service.useracounttype;

import com.talos.framework.common.pojo.PageResult;
import com.talos.framework.common.util.object.BeanUtils;
import com.talos.ums.controller.admin.useracounttype.vo.UmsUserAcountTypePageReqVO;
import com.talos.ums.controller.admin.useracounttype.vo.UmsUserAcountTypeSaveReqVO;
import com.talos.ums.dal.dataobject.useracounttype.UmsUserAcountTypeDO;
import com.talos.ums.dal.mysql.useracounttype.UmsUserAcountTypeMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import static com.talos.framework.common.exception.enums.GlobalErrorCodeConstants.DATA_NOT_FOUND;
import static com.talos.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 账号类型 Service 实现类
 *
 * @author ${table.author}
 */
@Service
@Validated
public class UmsUserAcountTypeServiceImpl implements UmsUserAcountTypeService {

    @Resource
    private UmsUserAcountTypeMapper userAcountTypeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createUserAcountType(UmsUserAcountTypeSaveReqVO createReqVO) {
        // 插入
        UmsUserAcountTypeDO userAcountType = BeanUtils.toBean(createReqVO, UmsUserAcountTypeDO.class);
        userAcountTypeMapper.insert(userAcountType);
        // 返回
        return userAcountType.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUserAcountType(UmsUserAcountTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateUserAcountTypeExists(updateReqVO.getId());
        // 更新
        UmsUserAcountTypeDO updateObj = BeanUtils.toBean(updateReqVO, UmsUserAcountTypeDO.class);
        userAcountTypeMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteUserAcountType(Long id) {
        // 校验存在
        validateUserAcountTypeExists(id);
        // 删除
        userAcountTypeMapper.deleteById(id);
    }

    private void validateUserAcountTypeExists(Long id) {
        if (userAcountTypeMapper.selectById(id) == null) {
            throw exception(DATA_NOT_FOUND);
        }
    }

    @Override
    public UmsUserAcountTypeDO getUserAcountType(Long id) {
        return userAcountTypeMapper.selectById(id);
    }

    @Override
    public PageResult<UmsUserAcountTypeDO> getUserAcountTypePage(UmsUserAcountTypePageReqVO pageReqVO) {
        return userAcountTypeMapper.selectPage(pageReqVO);
    }

}
